package com.scoy.honeymei.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.bean.FilmVipBean;
import com.scoy.honeymei.databinding.DialogTicketSendBinding;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.CheckUtil;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * ClassName:      com.shanchuang.propertycustomer.dialog
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/12/9/0009$ 下午 16:12$
 * UpdateRemark:   更新内容
 */
public class TicketSendDialog extends DialogFragment {


    private DialogTicketSendBinding binding;

    public static TicketSendDialog newInstance() {
        Bundle args = new Bundle();
        TicketSendDialog fragment = new TicketSendDialog();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); //无标题
        binding = DialogTicketSendBinding.inflate(inflater, container, false);


        initView();

        return binding.getRoot();
    }


    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        //  dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        if (dialogWindow != null) {
            dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
            dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            dialogWindow.setGravity(Gravity.CENTER);
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            lp.gravity = Gravity.CENTER;
            lp.windowAnimations = android.R.style.Animation_InputMethod;
            dialogWindow.setAttributes(lp);
        }
    }

    private void initView() {

        binding.dnSureBtn.setOnClickListener(v -> {

            preClick();
        });
        binding.dnCancelBtn.setOnClickListener(v -> {
            dismiss();
        });
    }

    private void preClick() {
        if (!MyUtil.isFastClick()) return;
        if (TextUtils.isEmpty(binding.dnPhoneEt.getText())) {
            MyUtil.mytoast0(getActivity(), "请输入手机号");
            return;
        }
        if (!CheckUtil.isMobileNO(binding.dnPhoneEt.getText().toString().trim())) {
            MyUtil.mytoast0(getActivity(), "请输入正确的手机号");
            return;
        }
        if (TextUtils.isEmpty(binding.dnNumEt.getText())) {
            MyUtil.mytoast0(getActivity(), "请输入转赠次数");
            return;
        }

        httpSend();
    }


    private void httpSend() {
        HttpParams params = new HttpParams();
        params.put("user_id", SPHelper.getString(MeConstant.UID, ""));
        params.put("to_mobile", binding.dnPhoneEt.getText().toString().trim());
        params.put("num", binding.dnNumEt.getText().toString().trim());
        HpGo.newInstance().HttpGo(getActivity(), MyUrl.FILM_TICKET_SEND, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                MyUtil.mytoast(getActivity(), "操作成功");
                onDialogBack.onBack();
                dismiss();
            }

        });

    }


    public interface OnDialogBack {
        void onBack();
    }

    public OnDialogBack onDialogBack;

    public TicketSendDialog setOnDialogBack(OnDialogBack onDialogBack) {
        this.onDialogBack = onDialogBack;

        return this;
    }
}
